package com.stox.client.ui.javafx.widget;

import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import javafx.util.StringConverter;

import com.stox.client.api.core.ClientFactory;
import com.stox.client.api.data.provider.ExchangeClient;
import com.stox.client.ui.javafx.core.StoxPlatform;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.i18n.MessageSource;
import com.stox.shared.model.Exchange;
import com.stox.shared.model.enumeration.ScripType;
import com.stox.shared.model.filter.ScripCriteria;
import com.stox.shared.util.StringUtil;

public class ScripCriteriaPanel extends VBox {
	
	private final ComboBox<Exchange> exchangeComboBox = new ComboBox<>();
	private final ComboBox<ScripType> scripTypeComboBox = new ComboBox<>();
	
	public ScripCriteriaPanel() {
		UI.style(this, CSS.scripFilterPanel);
		HBox.setHgrow(this, Priority.ALWAYS);
		VBox.setVgrow(this, Priority.NEVER);
		exchangeComboBox.setMaxWidth(Double.MAX_VALUE);
		scripTypeComboBox.setMaxWidth(Double.MAX_VALUE);
		getChildren().addAll(exchangeComboBox, scripTypeComboBox);
		
		exchangeComboBox.setConverter(new StringConverter<Exchange>(){
			@Override
			public Exchange fromString(String string) {
				return null;
			}
			@Override
			public String toString(Exchange exchange) {
				return exchange.getName();
			}
		});
		exchangeComboBox.setButtonCell(createExchangeListCell());
		exchangeComboBox.setCellFactory(new Callback<ListView<Exchange>, ListCell<Exchange>>() {
			public ListCell<Exchange> call(ListView<Exchange> param) {
				return createExchangeListCell();
			};
		});
				
		scripTypeComboBox.setConverter(new StringConverter<ScripType>(){
			@Override
			public ScripType fromString(String string) {
				return null;
			}
			
			@Override
			public String toString(ScripType scripType) {
				return MessageSource.get(scripType.getCode());
			}
		});
		scripTypeComboBox.getItems().setAll(ScripType.values());
		
		scripTypeComboBox.getSelectionModel().select(0);
		
		StoxPlatform.runLater(new Runnable(){
			@Override
			public void run() {
				exchangeComboBox.getItems().setAll(ClientFactory.getClient(ExchangeClient.class).findAll());
				exchangeComboBox.getSelectionModel().selectFirst();
			}
		});
	}
	
	private ListCell<Exchange> createExchangeListCell(){
		return new ListCell<Exchange>(){
			@Override
			protected void updateItem(Exchange item, boolean empty) {
				super.updateItem(item, empty);
				if(null != item && !empty){
					setText(item.getName());
					if(StringUtil.hasText(item.getIconImagePath())){
						setGraphic(new ImageView(new Image(item.getIconImagePath(), 20, 16,true, true)));
					}else{
						setGraphic(null);
					}
				}else{
					setGraphic(null);
					setText(null);
				}
				
			}
		};
	}
	
	public ScripCriteria value(ScripCriteria scripCriteria){
		scripCriteria.setExchangeId(exchangeComboBox.getValue().getId());
		scripCriteria.setScripType(scripTypeComboBox.getValue());
		return scripCriteria;
	}
	
	public ScripCriteria value(){
		return value(new ScripCriteria());
	}
	
	public ComboBox<Exchange> getExchangeComboBox() {
		return exchangeComboBox;
	}
	
	public ComboBox<ScripType> getScripTypeComboBox() {
		return scripTypeComboBox;
	}
}
